package com.scaffold.card.action;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.bean.BeanUtil;
import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.card.entity.Category;
import com.scaffold.card.entity.SimpleCard;
import com.scaffold.card.service.CategoryService;
import com.scaffold.card.service.SimpleCardService;

/**
 * 卡片操作
 * @author wangjg
 */
@Controller
public class SimpleCardAction extends BasicBizAction{
	@Autowired
	private SimpleCardService simpleCardService;
	@Autowired
	private CategoryService categoryService;
	
	/**
	 * 卡片列表
	 * @param keyword 查询关键字
	 */
	@RequestMapping("/simpleCard/list.xhtml")
	public String list(ModelMap model, Long categoryId, String name, String keyword){
		Page page = this.getPage(20);
		List<SimpleCard> list = simpleCardService.pageQuery(page, categoryId, keyword);
		model.put("simpleCardList", list);
		model.put("page", page);

		this.setEnumData(model, categoryId, name,keyword);

		return "/simpleCard/list.vm";
	}
	
	private void setEnumData(Map<String,Object> model, Long categoryId, String name, String keyword){
		List<Category> categoryList = categoryService.getCategoryEnum();
		model.put("categoryOptions", categoryList);	
	}

	/**
	 * 编辑卡片
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/simpleCard/edit.xhtml")
	public String edit(ModelMap model, Long id){
		if(id!=null){
			SimpleCard simpleCard = this.simpleCardService.getSimpleCard(id);
			this.checkOwner(simpleCard);
			model.put("simpleCard", simpleCard);	
			model.put("_update", "Y");
		}
		
		this.setEnumData(model, null, null, null);
		
		return "/simpleCard/edit.vm"; 
	}
	
	/**
	 * 保存卡片
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/simpleCard/save.xhtml")
	public String save(Long id){
		if(id==null){
			SimpleCard simpleCard = new SimpleCard();
			this.bindParams(simpleCard, "enabled");
			this.setOwner(simpleCard);
			this.simpleCardService.save(simpleCard);
		}else{
			SimpleCard simpleCard = this.simpleCardService.getSimpleCard(id);
			this.checkOwner(simpleCard);
			this.bindParams(simpleCard, "enabled");
			this.simpleCardService.update(simpleCard);
		}
		return "redirect:/simpleCard/list.xhtml";
	}
	
	/**
	 * 删除卡片
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/simpleCard/delete.xhtml")
	public String delete(Long id){
		SimpleCard simpleCard = this.simpleCardService.getSimpleCard(id);
		this.checkOwner(simpleCard);
		this.simpleCardService.delete(simpleCard);
		return "redirect:/simpleCard/list.xhtml";
	}

	// 前台展示
	@RequestMapping("/card/cardItem/{cardId}.xhtml")
	public String cardItem(ModelMap model, @PathVariable Long cardId){
		if(cardId==null){
			return null;
		}
		SimpleCard simpleCard = this.simpleCardService.getSimpleCard(cardId);
		this.checkOwner(simpleCard);
		model.put("simpleCard", simpleCard);
		
		List<Long> idList = simpleCardService.getCardIdList(simpleCard.getCategoryId());
		model.put("idList", idList);
		
		return "/card/cardItem.vm"; 
	}
	
	// 前台展示2
	@RequestMapping("/card/{categoryId}.xhtml")
	public String cardItem2(ModelMap model, @PathVariable Long categoryId){
		if(categoryId==null){
			return null;
		}
		Category category = this.categoryService.getCategory(categoryId);
		this.checkOwner(category);
		List<SimpleCard> list = simpleCardService.findByCategoryId(categoryId);
		List<Long> idList = BeanUtil.getDistinctPropertyList(list, "id");
		model.put("list", list);
		return "/card/cardItem2.vm"; 
	}

}
